package com.icesoft.system.auth.shiro;

import com.icesoft.framework.core.exception.BusinessException;
import com.icesoft.system.auth.entity.AuthToken;
import com.icesoft.system.entity.Account;
import com.icesoft.system.service.impl.AccountServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class CredentialsMatcher extends SimpleCredentialsMatcher {

	private final AccountServiceImpl accountService;

	//使用自定义token
	@Override
	public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
		AuthToken jwtToken = (AuthToken) token;
		if (!jwtToken.isValidPassword()) {
			return true;
		}
		if (jwtToken.getLoginUser().getAccount().getAuthSecret() == null) {
			throw new BusinessException("请输入密码");
		}
		Account account = jwtToken.getLoginUser().getAccount();
		//获得用户输入的密码:(可以采用加盐(salt)的方式去检验)
		String inPassword = account.getAuthSecret();
		String dbPassword = (String) info.getCredentials();
		//进行密码的比对
		return this.equals(inPassword, dbPassword);
	}

}
